package com.bluemsun.naturallanguageprocessing.controller;

import com.bluemsun.naturallanguageprocessing.common.ServerResponse;
import com.bluemsun.naturallanguageprocessing.entity.Favorites;
import com.bluemsun.naturallanguageprocessing.entity.UserApi;
import com.bluemsun.naturallanguageprocessing.service.ICollectService;
import com.bluemsun.naturallanguageprocessing.util.ConductRedisUtil;
import com.bluemsun.naturallanguageprocessing.vo.Collect1VO;
import com.bluemsun.naturallanguageprocessing.vo.CollectVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.*;

@Validated
@RestController
@RequestMapping("/collect")
public class CollectController {

    @Resource
    private ICollectService iCollectService;

    @Resource
    private ConductRedisUtil conductRedisUtil;

    // 用户查询收藏
    @GetMapping("/queryUserCollection")
    public ServerResponse<List<Favorites>> queryUserCollection(HttpServletRequest request){
        UserApi user = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        return iCollectService.queryUserCollection(user.getId());
    }
    // 用户进行收藏或取消收藏
    @PostMapping("/userDoCollect")
    public ServerResponse<Object> userDoCollect(HttpServletRequest request, @RequestBody @Validated CollectVO collectVO){
        UserApi user = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        return iCollectService.userDoCollect(user.getId(),collectVO.getFavoriteIds(),collectVO.getQuestionId());
    }
    // 用户查询收藏夹
    @GetMapping("/queryUserFavorites")
    public ServerResponse<Object> queryUserFavorites(HttpServletRequest request,
                                                     @RequestParam("questionId") @Min(1) Integer questionId){
        UserApi user = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        return iCollectService.queryUserFavorites(user.getId(),questionId);
    }
    // 用户修改文件夹名称
    @PostMapping("/modifyName")
    public ServerResponse<Object> modifyName(@RequestBody @Validated Favorites favorites, HttpServletRequest request){
        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        return iCollectService.modifyName(userApi.getId(), favorites.getId(), favorites.getName());
    }
    // 用户删除收藏夹
    @GetMapping("/deleteFavorite")
    public ServerResponse<Object> deleteFavorite(HttpServletRequest request,@Min(1) @RequestParam("favoriteId") Integer favoriteId){
        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        return iCollectService.deleteFavorite(userApi.getId(), favoriteId);
    }
    // 用户创建收藏夹
    @PostMapping("/createFavorite")
    public ServerResponse<Object> createFavorite(HttpServletRequest request,@RequestBody @Validated Favorites favorites){
        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        String favoritesName = favorites.getName();
        return iCollectService.createFavorite(userApi.getId(),favoritesName);
    }
    // 用户批量取消收藏
//    @GetMapping("/cancelCollected")
//    public ServerResponse<Object> cancelCollected(HttpServletRequest request,
//                                                  @RequestParam("favoriteId") @Min(1) Integer favoriteId,
//                                                  @RequestParam("questionIds") @NotEmpty ArrayList questionIds){
//        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
//        return iCollectService.cancelCollected(userApi.getId(), favoriteId, new HashSet<Integer>(questionIds));
//    }
    @PostMapping("/cancelCollected")
    public ServerResponse<Object> cancelCollected(HttpServletRequest request,
                                                  @RequestBody Collect1VO collectVO
                                                  ){
        UserApi userApi = conductRedisUtil.getUserFromRedis(request.getHeader("token"));
        return iCollectService.cancelCollected(userApi.getId(), collectVO.getFavoriteIds(), collectVO.getQuestionId());
    }


}
